{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 字典\n",
    "字典以关键字为索引，关键字通常是字符串或数字，也可以是其他任意不可变类型。\n",
    "只包含字符串、数字、元组的元组，也可以用作关键字。\n",
    "可以把字典理解为键值对的集合，字典的建必须是唯一的。花括号{}用于创建空字典。\n",
    "字典的主要用途是通过关键字存储、提取值。\n",
    "* 用del可以删除键值对。\n",
    "* 用已存在的关键字存储值，与该关键字关联的旧值会被取代。\n",
    "* 通过不存在的关键字提取值，则会报错。\n",
    "* 对字典执行list(d)操作，返回该字典所有键的列表，按插入次序排列。\n",
    "* 检查字典是否存在某个键，使用关键字in。"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'jack': 4098, 'sape': 4139}\n",
      "{'jack': 4098, 'sape': 4139, 'guido': 4127}\n",
      "4098\n",
      "{'jack': 4098, 'guido': 4127}\n",
      "{'jack': 4098, 'guido': 4127, 'irv': 4127}\n",
      "['jack', 'guido', 'irv']\n",
      "['guido', 'irv', 'jack']\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "tel = {'jack': 4098, 'sape': 4139}\n",
    "print(tel)\n",
    "# 添加键值对\n",
    "tel['guido'] = 4127\n",
    "print(tel)\n",
    "# 提取值\n",
    "print(tel['jack'])\n",
    "# 删除键值对\n",
    "del tel['sape']\n",
    "print(tel)\n",
    "# 添加键值对\n",
    "tel['irv']= 4127\n",
    "print(tel)\n",
    "# 列出字典键的列表\n",
    "print(list(tel))\n",
    "# 排序字典键的列表\n",
    "print(sorted(tel))\n",
    "print('guido' in tel)\n",
    "print('jack' not in tel)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "dict()构造函数可以直接用键值对序列创建字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sape': 4139, 'guido': 4127, 'jack': 1098}\n"
     ]
    }
   ],
   "source": [
    "tel = dict([('sape', 4139), ('guido', 4127), ('jack', 1098)])\n",
    "print(tel)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "使用字典推导式创建字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1: 1, 2: 4, 3: 9}\n"
     ]
    }
   ],
   "source": [
    "d = {x: x**2 for x in {1, 2, 3}}\n",
    "print(d)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "关键字是简单字符串时，直接用关键字参数指定键值对创建字典"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'sape': 4139, 'guido': 4127, 'jack': 4098}\n"
     ]
    }
   ],
   "source": [
    "d = dict(sape=4139, guido=4127, jack=4098)\n",
    "print(d)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 删除元素"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'worker_model': {'Qwen-7B-Chat': '123456', 'Qwen-14B-Chat': '223344'}, 'online_api': {'qwen-api': '21023', 'xinghuo-api': '21022'}}\n",
      "['worker_model', 'online_api']\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'fanxing-api'",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[4], line 5\u001B[0m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28mprint\u001B[39m(processes)\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;28mlist\u001B[39m(processes))\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;28;01mdel\u001B[39;00m processes[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124monline_api\u001B[39m\u001B[38;5;124m\"\u001B[39m][\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mfanxing-api\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28mprint\u001B[39m(processes)\n",
      "\u001B[1;31mKeyError\u001B[0m: 'fanxing-api'"
     ]
    }
   ],
   "source": [
    "processes = {\"worker_model\": {\"Qwen-7B-Chat\": \"123456\", \"Qwen-14B-Chat\": \"223344\"},\n",
    "             \"online_api\": {\"qwen-api\": \"21023\", \"xinghuo-api\": \"21022\"}}\n",
    "print(processes)\n",
    "print(list(processes))\n",
    "del processes[\"online_api\"][\"fanxing-api\"]\n",
    "print(processes)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 判断元素存在"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "online\n"
     ]
    }
   ],
   "source": [
    "params = {\"appid\": \"123456\", \"secret_key\": \"abcdef\", \"onlineApi\": \"false\"}\n",
    "\n",
    "if \"onlineApi\" in params and params[\"onlineApi\"]:\n",
    "    print(\"online\")\n",
    "else:\n",
    "    print(\"offline\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}